<script setup lang="ts">
import { reactive, ref } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import type { FormInstance, FormRules } from 'element-plus'
import { useRouter } from 'vue-router'
import { useUserStore } from '../../stores/user'
import type { LoginParams } from '../../api/user/types'

// 登录表单数据
const loginForm = reactive({
  username: '',
  password: '',
})

// 表单验证规则
const rules = reactive<FormRules>({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度应为3-20个字符', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度应为6-20个字符', trigger: 'blur' },
  ],
})

// 表单引用
const loginFormRef = ref<FormInstance>()

// 路由实例
const router = useRouter()

// 用户store
const userStore = useUserStore()

// 登录方法
const handleLogin = async (formEl: FormInstance | undefined) => {
  if (!formEl) return

  await formEl.validate(async (valid, fields) => {
    if (valid) {
      // 调用store的登录方法
      const loginParams: LoginParams = {
        username: loginForm.username,
        password: loginForm.password,
      }

      const success = await userStore.login(loginParams)

      if (success) {
        // 登录成功，跳转到dashboard页面
        setTimeout(() => {
          router.push('/dashboard')
        }, 1000)
      }
    } else {
      console.log('表单验证失败', fields)
    }
  })
}

// 重置表单
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>

<template>
  <div class="login_container">
    <!-- 登录表单 -->
    <div class="login_form">
      <div class="login_form_content">
        <h2 class="login_title">用户登录</h2>

        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="rules"
          size="large"
          status-icon
        >
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              :prefix-icon="User"
              clearable
            />
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              :prefix-icon="Lock"
              show-password
              clearable
            />
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              size="large"
              :loading="userStore.loading"
              @click="handleLogin(loginFormRef)"
              style="width: 100%"
            >
              {{ userStore.loading ? '登录中...' : '登录' }}
            </el-button>
          </el-form-item>

          <el-form-item>
            <el-button
              size="large"
              @click="resetForm(loginFormRef)"
              style="width: 100%"
            >
              重置
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style scoped>
/* 重置样式，防止出现滚动条 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.login_container {
  width: 100vw;
  height: 100vh;
  background: url('../../assets/images/background.jpg') no-repeat center center;
  background-size: cover;
  background-attachment: fixed;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding: 0 80px;
  position: relative;
  overflow: hidden;
  box-sizing: border-box;
}

.login_form {
  width: 400px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.login_form_content {
  padding: 40px;
}

.login_title {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
  font-size: 24px;
  font-weight: 600;
}

/* 表单项样式 */
:deep(.el-form-item) {
  margin-bottom: 24px;
}

:deep(.el-input__wrapper) {
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

:deep(.el-button) {
  border-radius: 8px;
  font-weight: 500;
  transition: all 0.3s ease;
}

:deep(.el-button--primary) {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
}

:deep(.el-button--primary:hover) {
  background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%);
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(102, 126, 234, 0.4);
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .login_container {
    padding: 0 40px;
  }
}

@media (max-width: 768px) {
  .login_container {
    padding: 20px;
    justify-content: center;
    background-attachment: scroll;
  }

  .login_form {
    width: 100%;
    max-width: 400px;
    margin: 0 auto;
  }

  .login_form_content {
    padding: 30px 20px;
  }
}

@media (max-width: 480px) {
  .login_container {
    padding: 15px;
    height: 100vh;
    min-height: 600px;
  }

  .login_form {
    max-width: 100%;
    margin: 0;
  }

  .login_form_content {
    padding: 25px 15px;
  }

  .login_title {
    font-size: 20px;
    margin-bottom: 25px;
  }
}
</style>
